var Src_AreaGroup_Num = [];
var Dest_AreaGroup_Num = [];
//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}
function addNodeSrc() {
    var form = $("#myModal .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var Src_AreaGroup_ID = $("#Src_AreaGroup_ID").val();
    var AS_ID = $("#AS_ID").val().split("\n");
    var Area_Name = $("#Area_Name").val().split("\n");
    var srcObj = {
        Src_AreaGroup_ID: Src_AreaGroup_ID,
        AS_IDs: {AS_ID: AS_ID},
        Areas: {Area: Area_Name}
    };
    Src_AreaGroup_Num.push(srcObj);
    showSrc();
    $('#myModal').modal('hide')
}
function editSrc(num) {
    $("#Src_AreaGroup_ID").val(Src_AreaGroup_Num[num].Src_AreaGroup_ID);
    $("#AS_ID").val(Src_AreaGroup_Num[num].AS_IDs.AS_ID.join("\n"));
    $("#Area_Name").val(Src_AreaGroup_Num[num].Areas.Area.join("\n"));
    $('#myModal').modal('show');
    $(".addsrcnode").attr("onclick", "editNodeSrc(" + num + ")");
}
function editNodeSrc(num) {
    var Src_AreaGroup_ID = $("#Src_AreaGroup_ID").val();
    var AS_ID = $("#AS_ID").val().split("\n");
    var Area_Name = $("#Area_Name").val().split("\n");
    // var srcObj = {
    //     Src_AreaGroup_ID: Src_AreaGroup_ID,
    //     AS_IDs: {AS_ID: AS_ID},
    //     Areas: {Area: Area_Name}
    // };
    Src_AreaGroup_Num[num].Src_AreaGroup_ID = Src_AreaGroup_ID;
    Src_AreaGroup_Num[num].AS_IDs = {AS_ID: AS_ID};
    Src_AreaGroup_Num[num].Areas = {Area: Area_Name};
    showSrc();
    $('#myModal').modal('hide')
}
function delSrc(num) {
    Src_AreaGroup_Num.splice(num, 1);
    showSrc();
}
function showSrc() {
    var obj = $(".Src_AreaGroup_Num tbody");
    var cont = "";
    var number = 0;
    for (var i in Src_AreaGroup_Num) {
        cont += "<tr>";
        cont += "<td>" + Src_AreaGroup_Num[i].Src_AreaGroup_ID + "</td>";
        cont += "<td>" + Src_AreaGroup_Num[i].AS_IDs.AS_ID.join(",") + "</td>";
        cont += "<td><a href='javascript:editSrc(" + number + ")'>&nbsp;编&nbsp;辑&nbsp;</a>/<a href='javascript:delSrc(" + number + ")'>&nbsp;删&nbsp;除&nbsp;</a></td>";
        cont += "</tr>";
        number++;
    }
    obj.html("");
    obj.append(cont);
}
function addNodeDest() {
    var form = $("#myModal2 .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var Dest_AreaGroup_ID = $("#Dest_AreaGroup_ID").val();
    var AS_ID = $("#Dest_AS_ID").val().split("\n");
    var Area_Name = $("#Dest_Area_Name").val().split("\n");
    var destObj = {
        Dest_AreaGroup_ID: Dest_AreaGroup_ID,
        AS_IDs: {AS_ID: AS_ID},
        Areas: {Area: Area_Name}
    };
    Dest_AreaGroup_Num.push(destObj);
    showDest();
    $('#myModal2').modal('hide')
}
function editDest(num) {
    $("#Dest_AreaGroup_ID", $('#myModal2')).val(Dest_AreaGroup_Num[num].Dest_AreaGroup_ID);
    $("#Dest_AS_ID", $('#myModal2')).val(Dest_AreaGroup_Num[num].AS_IDs.AS_ID.join("\n"));
    $("#Dest_Area_Name", $('#myModal2')).val(Dest_AreaGroup_Num[num].Areas.Area.join("\n"));
    $('#myModal2').modal('show');
    $(".adddestnode", $('#myModal2')).attr("onclick", "editNodeDest(" + num + ")");
}
function editNodeDest(num) {
    var Dest_AreaGroup_ID = $("#Dest_AreaGroup_ID").val();
    var AS_ID = $("#Dest_AS_ID").val().split("\n");
    var Area_Name = $("#Dest_Area_Name").val().split("\n");
    var destObj = {
        Dest_AreaGroup_ID: Dest_AreaGroup_ID,
        AS_IDs: {AS_ID: AS_ID},
        Areas: {Area: Area_Name}
    };
    Dest_AreaGroup_Num[num].Dest_AreaGroup_ID = Dest_AreaGroup_ID;
    Dest_AreaGroup_Num[num].AS_IDs = {AS_ID: AS_ID};
    Dest_AreaGroup_Num[num].Areas = {Area: Area_Name};
    showDest();
    $('#myModal2').modal('hide')
}
function delDest(num) {
    Dest_AreaGroup_Num.splice(num, 1);
    showDest();
}
function showDest() {
    var obj = $(".Dest_AreaGroup_Num tbody");
    var cont = "";
    var number = 0;
    for (var i in Dest_AreaGroup_Num) {
        cont += "<tr>";
        cont += "<td>" + Dest_AreaGroup_Num[i].Dest_AreaGroup_ID + "</td>";
        cont += "<td>" + Dest_AreaGroup_Num[i].AS_IDs.AS_ID.join(",") + "</td>";
        cont += "<td><a href='javascript:editDest(" + number + ")'>&nbsp;编&nbsp;辑&nbsp;</a>/<a href='javascript:delDest(" + number + ")'>&nbsp;删&nbsp;除&nbsp;</a></td>";
        cont += "</tr>";
        number++;
    }
    obj.html("");
    obj.append(cont);
}
//添加数据
function save() {
    var form = $(".main-container .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    if (Src_AreaGroup_Num.length < 1 || Dest_AreaGroup_Num.length < 1) {
        alert("源和目的区域请至少各填写一组!")
    } else {
        var comment = $(".policy_desc").val();
        for(var i in Src_AreaGroup_Num){
            if(Src_AreaGroup_Num[i].AS_IDs.AS_ID[0]==""){
                Src_AreaGroup_Num[i].AS_IDs=null;
            }
            if(Src_AreaGroup_Num[i].Areas.Area[0]==""){
                Src_AreaGroup_Num[i].Areas=null;
            }
        }
        for(var i in Dest_AreaGroup_Num){
            if(Dest_AreaGroup_Num[i].AS_IDs.AS_ID[0]==""){
                Dest_AreaGroup_Num[i].AS_IDs=null;
            }
            if(Dest_AreaGroup_Num[i].Areas.Area[0]==""){
                Dest_AreaGroup_Num[i].Areas=null;
            }
        }
        for(var i in Src_AreaGroup_Num){
            var obj = {
                group_name:Src_AreaGroup_Num[i].Src_AreaGroup_ID,
                areas:Src_AreaGroup_Num[i].Areas.Area
            };
            $.ajax({
                type: "POST",
                async: false,
                url: "../../action/traffic_flow/create_areas.do",
                data: {json:JSON.stringify(obj)},
                dataType: "json",
                success: function(data) {
                    Src_AreaGroup_Num[i].Src_AreaGroup_ID = data.group_id;
                }
            });
        }
        for(var i in Dest_AreaGroup_Num){
            var obj = {
                group_name:Dest_AreaGroup_Num[i].Dest_AreaGroup_ID,
                areas:Dest_AreaGroup_Num[i].Areas.Area
            };
            $.ajax({
                type: "POST",
                async: false,
                url: "../../action/traffic_flow/create_areas.do",
                data: {json:JSON.stringify(obj)},
                dataType: "json",
                success: function(data) {
                    Dest_AreaGroup_Num[i].Dest_AreaGroup_ID = data.group_id;
                }
            });
        }
        var parameter = {
            Src_AreaGroups: {Src_AreaGroup: Src_AreaGroup_Num}, //Src_AreaGroup_Num,
            Dest_AreaGroups: {Dest_AreaGroup: Dest_AreaGroup_Num}, //Dest_AreaGroup_Num,
        }
        var policy = JSON.stringify(parameter);
        $.ajax({
            type: "POST",
            async: false,
            url: "../../action/policy/create.do",
            data: {comment: comment, policy: policy, type: type, instance: DepotName},
            dataType: "json",
            success: function(data) {
                if (data.result == "true") {
                    alert("添加成功！");
                    previous();
                } else {
                    alert("添加失败!")
                }
            }
        });
    }
}
//编辑数据
function showData() {
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/get.do",
        data: {type: type, messageNo: id, instance: DepotName},
        dataType: "json",
        success: function(souData) {
            //souData = $.parseJSON(souData);
            var policy_desc = souData.Comment;
            $(".policy_desc").val(policy_desc);
            var data = souData.Content;            
            Src_AreaGroup_Num = data.Src_AreaGroups.Src_AreaGroup;
            Dest_AreaGroup_Num = data.Dest_AreaGroups.Dest_AreaGroup;
            for(var i in Src_AreaGroup_Num){
                if(Src_AreaGroup_Num[i].AS_IDs==null){
                    Src_AreaGroup_Num[i].AS_IDs = {
                        "AS_ID": []
                    }
                }
                if(Src_AreaGroup_Num[i].Areas==null){
                    Src_AreaGroup_Num[i].Areas = {
                        "Area": []
                    }
                }
            }
            for(var i in Dest_AreaGroup_Num){
                if(Dest_AreaGroup_Num[i].AS_IDs==null){
                    Dest_AreaGroup_Num[i].AS_IDs = {
                        "AS_ID": []
                    }
                }
                if(Dest_AreaGroup_Num[i].Areas==null){
                    Dest_AreaGroup_Num[i].Areas = {
                        "Area": []
                    }
                }
            }
            for(var i in Src_AreaGroup_Num){
                var obj = {
                    group_id:Src_AreaGroup_Num[i].Src_AreaGroup_ID
                };
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "../../action/traffic_flow/query_areas.do",
                    data: {json:JSON.stringify(obj)},
                    dataType: "json",
                    success: function(data) {
                        Src_AreaGroup_Num[i]["old_Src_AreaGroup_ID"] = Src_AreaGroup_Num[i].Src_AreaGroup_ID;
                        Src_AreaGroup_Num[i].Src_AreaGroup_ID = data.group_name;
                        Src_AreaGroup_Num[i]["old_Src_AreaGroup_Name"] = data.group_name;
                    }
                });
            }
            for(var i in Dest_AreaGroup_Num){
                var obj = {
                    group_id:Dest_AreaGroup_Num[i].Dest_AreaGroup_ID
                };
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "../../action/traffic_flow/query_areas.do",
                    data: {json:JSON.stringify(obj)},
                    dataType: "json",
                    success: function(data) {
                        Dest_AreaGroup_Num[i]["old_Dest_AreaGroup_ID"] = Dest_AreaGroup_Num[i].Dest_AreaGroup_ID;
                        Dest_AreaGroup_Num[i].Dest_AreaGroup_ID = data.group_name;
                        Dest_AreaGroup_Num[i]["old_Dest_AreaGroup_Name"] = data.group_name;
                    }
                });
            }
            showSrc();
            showDest();
        }
    });
}
function edit() {
    var form = $(".main-container .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    if (Src_AreaGroup_Num.length < 1 || Dest_AreaGroup_Num.length < 1) {
        alert("源和目的区域请至少各填写一组!")
    } else {
        var comment = $(".policy_desc").val();
        for(var i in Src_AreaGroup_Num){
            if(Src_AreaGroup_Num[i].AS_IDs.AS_ID[0]==""){
                Src_AreaGroup_Num[i].AS_IDs=null;
            }
            if(Src_AreaGroup_Num[i].Areas.Area[0]==""){
                Src_AreaGroup_Num[i].Areas=null;
            }
        }
        for(var i in Dest_AreaGroup_Num){
            if(Dest_AreaGroup_Num[i].AS_IDs.AS_ID[0]==""){
                Dest_AreaGroup_Num[i].AS_IDs=null;
            }
            if(Dest_AreaGroup_Num[i].Areas.Area[0]==""){
                Dest_AreaGroup_Num[i].Areas=null;
            }
        }
        for(var i in Src_AreaGroup_Num){
            if(typeof(Src_AreaGroup_Num[i].old_Src_AreaGroup_Name)=="undefined"){
                var obj = {
                    group_name:Src_AreaGroup_Num[i].Src_AreaGroup_ID,
                    areas:Src_AreaGroup_Num[i].Areas.Area
                };
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "../../action/traffic_flow/create_areas.do",
                    data: {json:JSON.stringify(obj)},
                    dataType: "json",
                    success: function(data) {
                        Src_AreaGroup_Num[i].Src_AreaGroup_ID = data.group_id;
                    }
                });
            }else{
                var obj = {
                    old_group_id:Src_AreaGroup_Num[i].old_Src_AreaGroup_ID,
                    new_group_name:Src_AreaGroup_Num[i].Src_AreaGroup_ID,
                    areas:Src_AreaGroup_Num[i].Areas.Area
                };
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "../../action/traffic_flow/edit_areas.do",
                    data: {json:JSON.stringify(obj)},
                    dataType: "json",
                    success: function(data) {
                        Src_AreaGroup_Num[i].Src_AreaGroup_ID = Src_AreaGroup_Num[i].old_Src_AreaGroup_ID;
                        delete Src_AreaGroup_Num[i].old_Src_AreaGroup_ID;
                        delete Src_AreaGroup_Num[i].old_Src_AreaGroup_Name;
                    }
                });
            }
        }
        for(var i in Dest_AreaGroup_Num){
            if(typeof(Dest_AreaGroup_Num[i].old_Dest_AreaGroup_Name)=="undefined"){
                var obj = {
                    group_name:Dest_AreaGroup_Num[i].Dest_AreaGroup_ID,
                    areas:Dest_AreaGroup_Num[i].Areas.Area
                };
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "../../action/traffic_flow/create_areas.do",
                    data: {json:JSON.stringify(obj)},
                    dataType: "json",
                    success: function(data) {
                        Dest_AreaGroup_Num[i].Dest_AreaGroup_ID = data.group_id;
                    }
                });
            }else{
                var obj = {
                    old_group_id:Dest_AreaGroup_Num[i].old_Dest_AreaGroup_ID,
                    new_group_name:Dest_AreaGroup_Num[i].Dest_AreaGroup_ID,
                    areas:Dest_AreaGroup_Num[i].Areas.Area
                };
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "../../action/traffic_flow/edit_areas.do",
                    data: {json:JSON.stringify(obj)},
                    dataType: "json",
                    success: function(data) {
                        Dest_AreaGroup_Num[i].Dest_AreaGroup_ID = Dest_AreaGroup_Num[i].old_Dest_AreaGroup_ID;
                        delete Dest_AreaGroup_Num[i].old_Dest_AreaGroup_ID;
                        delete Dest_AreaGroup_Num[i].old_Dest_AreaGroup_Name;
                    }
                });
            }
        }
        var parameter = {
            Src_AreaGroups: {Src_AreaGroup: Src_AreaGroup_Num}, //Src_AreaGroup_Num,
            Dest_AreaGroups: {Dest_AreaGroup: Dest_AreaGroup_Num}, //Dest_AreaGroup_Num,
        }
        var policy = JSON.stringify(parameter);
        $.ajax({
            type: "POST",
            async: false,
            url: "../../action/policy/update.do",
            data: {messageNo: id, comment: comment, policy: policy, type: type, instance: DepotName},
            dataType: "json",
            success: function(data) {
                if (data.result == "true") {
                    alert("编辑成功！");
                    previous();
                } else {
                    alert("编辑失败!")
                }
            }
        });
    }
}
//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}
var base64 = new Base64();
var type = getQueryString("type");
var DepotID = getQueryString("DepotID");
var DepotName = getQueryString("DepotName");
DepotName = base64.decode(DepotName);
var id = getQueryString("id");
$(function() {
// console.log("type:" + type + "\tDepotID:" + DepotID + "\tDepotName:" + DepotName + "\tid:" + id)
    $(".header").text(DepotName + " 策略管理");
    $(".Src_AreaGroup").click(function() {
        $('#myModal').modal('show');
        $(".addsrcnode").attr("onclick", "addNodeSrc()");
    });
    $(".Dest_AreaGroup").click(function() {
        // $('#myModal').modal('hide');
        $('#myModal2').modal('show');
        $(".adddestnode").attr("onclick", "addNodeDest()");
    });
// setInterval(function () {
// parent.$("#page-content").attr("height", $("html").height());
// }, 500);
});
/*
 * 查看旧版本的策略内容
 */
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");

    var policy_desc = souData.Comment;
    $(".policy_desc").val(policy_desc);
    var data = souData.Content;
    Src_AreaGroup_Num = data.Src_AreaGroups.Src_AreaGroup;
    Dest_AreaGroup_Num = data.Dest_AreaGroups.Dest_AreaGroup;
    for(var i in Src_AreaGroup_Num){
        if(Src_AreaGroup_Num[i].AS_IDs==null){
            Src_AreaGroup_Num[i].AS_IDs = {
                "AS_ID": []
            }
        }
        if(Src_AreaGroup_Num[i].Areas==null){
            Src_AreaGroup_Num[i].Areas = {
                "Area": []
            }
        }
    }
    for(var i in Dest_AreaGroup_Num){
        if(Dest_AreaGroup_Num[i].AS_IDs==null){
            Dest_AreaGroup_Num[i].AS_IDs = {
                "AS_ID": []
            }
        }
        if(Dest_AreaGroup_Num[i].Areas==null){
            Dest_AreaGroup_Num[i].Areas = {
                "Area": []
            }
        }
    }
    for(var i in Src_AreaGroup_Num){
        var obj = {
            group_id:Src_AreaGroup_Num[i].Src_AreaGroup_ID
        };
        $.ajax({
            type: "POST",
            async: false,
            url: "../../action/traffic_flow/query_areas.do",
            data: {json:JSON.stringify(obj)},
            dataType: "json",
            success: function(data) {
                Src_AreaGroup_Num[i].Src_AreaGroup_ID = data.group_name;
            }
        });
    }
    for(var i in Dest_AreaGroup_Num){
        var obj = {
            group_id:Dest_AreaGroup_Num[i].Dest_AreaGroup_ID
        };
        $.ajax({
            type: "POST",
            async: false,
            url: "../../action/traffic_flow/query_areas.do",
            data: {json:JSON.stringify(obj)},
            dataType: "json",
            success: function(data) {
                Dest_AreaGroup_Num[i].Dest_AreaGroup_ID = data.group_name;
            }
        });
    }
    showSrc();
    showDest();
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}